package com.xbsg.protocol.xf.service;

import cn.hutool.core.util.IdUtil;
import com.xbsg.protocol.xf.entiy.FireUploadData;
import com.xbsg.protocol.xf.entiy.FireUploadDataOboject;
import com.xbsg.protocol.xf.mapper.FireUploadDataMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 消防上传数据Service业务层处理
 *
 * @author zmc
 * @date 2023-03-01
 */
@Service
@Slf4j
public class FireUploadDataServiceImpl {
    @Autowired
    private FireUploadDataMapper fireUploadDataMapper;

    /**
     * 查询消防上传数据
     *
     * @param id 消防上传数据ID
     * @return 消防上传数据
     */
    public FireUploadData selectFireUploadDataById(String id) {
        return fireUploadDataMapper.selectFireUploadDataById(id);
    }

    /**
     * 查询消防上传数据列表
     *
     * @param fireUploadData 消防上传数据
     * @return 消防上传数据
     */
    public List<FireUploadData> selectFireUploadDataList(FireUploadData fireUploadData) {
        return fireUploadDataMapper.selectFireUploadDataList(fireUploadData);
    }

    /**
     * 新增消防上传数据
     *
     * @param fireUploadData 消防上传数据
     * @return 结果
     */
    @Transactional
    public int insertFireUploadData(FireUploadData fireUploadData) {
        fireUploadData.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        int rows = fireUploadDataMapper.insertFireUploadData(fireUploadData);
        insertFireUploadDataOboject(fireUploadData);
        return rows;
    }

    /**
     * 新增消防上传应用数据信息
     *
     * @param fireUploadData 消防上传数据对象
     */
    public void insertFireUploadDataOboject(FireUploadData fireUploadData) {
        List<FireUploadDataOboject> fireUploadDataObojectList = fireUploadData.getFireUploadDataObojectList();
        String id = fireUploadData.getId();
        if (fireUploadDataObojectList != null) {
            List<FireUploadDataOboject> list = new ArrayList<FireUploadDataOboject>();
            for (FireUploadDataOboject fireUploadDataOboject : fireUploadDataObojectList){
                fireUploadDataOboject.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
                fireUploadDataOboject.setDataId(id);
                list.add(fireUploadDataOboject);
            }
            if (list.size() > 0) {
                fireUploadDataMapper.batchFireUploadDataOboject(list);
            }
        }
    }
}
